package com.cpi.cornpp.plants.services;

import java.util.ArrayList;
import java.util.List;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.plants.converter.PlantConverter;
import com.cpi.cornpp.plants.dao.PlantDAO;
import com.cpi.cornpp.plants.dto.PlantDTO;
import com.cpi.cornpp.plants.exception.PlantException;
import com.cpi.cornpp.plants.view.PlantView;

/**
 * Plant Service
 * @author Edgar Joao
 *
 */
public class PlantService {

	private static final String PLANT_EXISTS = "lable.plants.error.duplicate";

	/**
	 * @author Edgar Joao
	 * @return Plant View List
	 * @throws PlantException
	 */
	public List list(int orderField, boolean ascendingOrder) throws PlantException{	
		PlantDAO plantDAO = null;
		PlantConverter pConverter = null;
		List plantDTOs = null;
		List plantViews = null;
						
		try {
			plantDAO = new PlantDAO();
			plantDTOs = new ArrayList(0);
			plantViews = new ArrayList(0);
			pConverter = new PlantConverter();
			
			plantDTOs = plantDAO.list(orderField, ascendingOrder);			
			plantViews = pConverter.convertDTOs(plantDTOs);
			
			
		} catch (PlantException plantException) {
			throw plantException;
		}catch (Exception e){
			PlantException plantException = null;
			e.printStackTrace(System.out);
			plantException = new PlantException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw plantException;
		}			
		
		return plantViews;
	}
	
	/**
	 * @author Edgar Joao
	 * @return Plant View List
	 * @throws PlantException
	 */
	public List list() throws PlantException{	
		PlantDAO plantDAO = null;
		PlantConverter pConverter = null;
		List plantDTOs = null;
		List plantViews = null;
						
		try {
			plantDAO = new PlantDAO();
			plantDTOs = new ArrayList(0);
			plantViews = new ArrayList(0);
			pConverter = new PlantConverter();
			
			plantDTOs = plantDAO.list();			
			plantViews = pConverter.convertDTOs(plantDTOs);
			
			
		} catch (PlantException plantException) {
			plantException.printStackTrace(System.out);
			plantException= new PlantException(plantException, CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw plantException;
		}catch (Exception e){
			PlantException plantException = null;
			e.printStackTrace(System.out);
			plantException = new PlantException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw plantException;
		}			
		
		return plantViews;
	}
	
	/**
	 * Add a new Plant
	 * @author Edgar Joao
	 */
	public void addPlant(PlantView plantView) throws PlantException {
		PlantDAO plantDAO = null;
		PlantConverter plantConverter = null;
		PlantDTO plantDTO = null;

		if (plantView != null) {
			plantDAO = new PlantDAO();
			plantConverter = new PlantConverter();

			plantDTO = plantConverter.converterPlantViewToDTO(plantView);

			try {
				if( plantDAO.getPlant(plantDTO) == null ){
					plantDAO.insert(plantDTO);
				}else{
					throw new PlantException(PLANT_EXISTS, PlantException.LAYER_SERVICE, PlantException.ACTION_INSERT);
				}
			} catch (PlantException pe) {
				throw pe;
			} catch (Exception e) {
				PlantException pe = new PlantException(e, PlantException.LAYER_SERVICE, PlantException.ACTION_INSERT);
				e.printStackTrace(System.out);
				throw pe;
			}
		}
	}
	
	
	/**
	 * Update Plants
	 * @author Edgar Joao
	 * @param plantView
	 * @throws PlantException
	 */
	public void updatePlant(PlantView plantView) throws PlantException{
		PlantDAO plantDAO = null;
		PlantConverter plantConverter = null;
		PlantDTO plantDTO = null;

		if (plantView != null) {
			plantDAO = new PlantDAO();
			plantConverter = new PlantConverter();

			plantDTO = plantConverter.converterPlantViewToDTO(plantView);

			try {
				plantDAO.update(plantDTO);
			} catch (PlantException pe) {
				throw pe;
			} catch (Exception e) {
				PlantException pe = new PlantException(e,
						PlantException.LAYER_SERVICE,
						PlantException.ACTION_INSERT);
				e.printStackTrace(System.out);
				throw pe;
			}
		}
	}
	
	/**
	 * Delete Plants
	 * @author Edgar Joao
	 * @param plantView
	 * @throws PlantException
	 */
	public void deletePlant(PlantView plantView) throws PlantException{
		PlantDAO plantDAO = null;
		PlantConverter plantConverter = null;
		PlantDTO plantDTO = null;

		if (plantView != null) {
			plantDAO = new PlantDAO();
			plantConverter = new PlantConverter();

			plantDTO = plantConverter.converterPlantViewToDTO(plantView);

			try {
				plantDAO.delete(plantDTO);
			} catch (PlantException pe) {
				throw pe;
			} catch (Exception e) {
				PlantException pe = new PlantException(e,
						PlantException.LAYER_SERVICE,
						PlantException.ACTION_INSERT);
				e.printStackTrace(System.out);
				throw pe;
			}
		}
	}
	
	/**
	 * Exist Plant
	 * @author Edgar Joao
	 * @return
	 * @throws PlantException
	 */
	public boolean existPlant(PlantView plantView) throws PlantException{
		PlantDAO plantDAO = null;
		PlantConverter plantConverter = null;
		PlantDTO plantDTO = null;
		boolean exist = false;
		
		if (plantView != null) {
			plantDAO = new PlantDAO();
			plantConverter = new PlantConverter();

			plantDTO = plantConverter.converterPlantViewToDTO(plantView);
			
			try {
				
				if(plantDAO.existPlant(plantDTO)){
					exist = true;	
				}else{
					exist = false;
				}
								
			} catch (PlantException pe) {
				throw pe;
			} catch (Exception e) {
				PlantException pe = new PlantException(e,
						PlantException.LAYER_SERVICE,
						PlantException.ACTION_INSERT);
				e.printStackTrace(System.out);
				throw pe;
			}			
		}
		return exist;
	}
	
	/**
	 * @author Edgar Joao
	 * @param plantView
	 * @return
	 * @throws PlantException
	 */
	public PlantView getPlant(PlantView plantView) throws PlantException{
		PlantDAO plantDAO = null;
		PlantConverter plantConverter = null;
		PlantDTO plantDTO = null;
		PlantView view = null;	
		
		if (plantView != null) {
			plantDAO = new PlantDAO();
			plantConverter = new PlantConverter();
			plantDTO = plantConverter.converterPlantViewToDTO(plantView);			
			try {
				view = new PlantView();	
				view = plantConverter.converterPlantDtoToView(plantDAO.getPlant(plantDTO));												
			} catch (PlantException pe) {
				throw pe;
			} catch (Exception e) {
				PlantException pe = new PlantException(e,
						PlantException.LAYER_SERVICE,
						PlantException.ACTION_INSERT);
				e.printStackTrace(System.out);
				throw pe;
			}			
		}
		return view;
	}
	
}
